package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Tag;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.query.TagQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 注册用户资源控制器
 */
@RestController
@RequestMapping("/strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private ITagService tagService;

    /**
     * 获取大攻略推荐页面数据（不分页）
     * @param queryObject
     * @return
     */
    @GetMapping
    public PageInfo query(StrategyQueryObject queryObject) {
        queryObject.setPageSize(0);
        return strategyService.query(queryObject);
    }

    /**
     * 根据大攻略id获取大攻略信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Strategy getStrategy(@PathVariable Long id) {
        return strategyService.selectByStrategyId(id);
    }

    /**
     * 根据大攻略id获取攻略目录和攻略文章
     * @param queryObject
     * @return
     */
    @GetMapping("/{strategyId}/catalogs")
    public PageInfo getStrategy(StrategyCatalogQueryObject queryObject) {
        queryObject.setPageSize(0);
        queryObject.setOrderBy("sc.sequence asc");
        PageInfo pageInfo = strategyCatalogService.query(queryObject);
        return pageInfo;
    }

    /**
     * 获取评论列表
     * @param queryObject
     * @return
     */
    @GetMapping("/{strategyId}/comments")
    public PageInfo getCommentsByStrategyId(StrategyCommentQueryObject queryObject) {
        queryObject.setOrderBy("sc.createTime desc");
        PageInfo pageInfo = strategyCommentService.query(queryObject);
        return pageInfo;
    }


    /**
     * 保存评论
     * @param strategyComment
     * @param tags
     * @return
     */
    @PostMapping("/{strategy.id}/comments")
    public JSONResult save(StrategyComment strategyComment,String[]tags) {
        strategyCommentService.save(strategyComment ,tags);
        return new JSONResult();
    }

    /**
     * 根据攻略id获取攻略评论热门标签
     * @param queryObject
     * @return
     */
    @GetMapping("/{strategyId}/tags")
    public PageInfo getTagsByStrategyId(TagQueryObject queryObject) {
        queryObject.setOrderBy("num desc");
        PageInfo pageInfo = tagService.query(queryObject);
        return pageInfo;
    }


}
